package com.chanyachting.emall.action.newsactivity;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Newsactivity;
import com.chanyachting.emall.form.NewsActivityForm;
import com.chanyachting.emall.service.NewsActivityService;
import com.chanyachting.emall.serviceImpl.NewsActivityServiceImpl;
import com.chanyachting.emall.util.MyFile;
import com.chanyachting.emall.util.RequestUtil;

public class UpdateAction extends Action {

	
	
	//更新新闻活动
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		if(request.getMethod().equals("POST")){
			
			NewsActivityForm naForm=(NewsActivityForm)form;
			
			NewsActivityService naservice=new NewsActivityServiceImpl();
			
			Newsactivity na=new Newsactivity();
			
			String imgUrl=MyFile.saveFileForm(naForm.getFile(), request);	
			System.out.println("UpdateAction:imgUrl=" + imgUrl);
			BeanUtils.copyProperties(na, naForm);
			System.out.println("UpdateAction@copyProperties after:imgUrl=" + imgUrl);
		
			if(imgUrl!=null && !imgUrl.trim().equals(""))  {
				na.setImgUrl(imgUrl);  //如果有图片更新
			}
			
			boolean temp=naservice.updateNewsActivity(na);
			
			if(temp){
				
				ru.setMsg("更新成功！");
				
			}else{
				
				ru.setMsg("更新失败！请稍候重试。");
			}

		}
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		//重新加载新闻活动对象
		Newsactivity na=new NewsActivityServiceImpl().getOneNewsActivity(id);
		
		ru.setAttribute("newsActivity", na);
		
		return mapping.findForward("updatenewsactivity");
	}
	
	
	
	

}
